from google.appengine.ext import db
from models.Messages import *

from google.appengine.ext.db import djangoforms

class Topic(db.Model):
    """This is the model class that stores Topic's in our db. 
    it contains 3 simple properties the name the author and the 
    date_created. It also contains a reference to the group that owns 
    the topic.  """
    topic = db.StringProperty(required=True)
    author = db.IntegerProperty(required=False)
    date_created = db.DateTimeProperty(required=True, auto_now_add=True)
    
    # reference id for the group that owns the topic
    group_id = db.IntegerProperty(required=False)
    
    def delete(self):
      """override the delete method to remove all messages associated with a
       topic"""
      # fetch all the assocated messages
      query = Message.all()
      query.filter("topic = ", self.key().id())
       
       # iterate through all messages to delete them
      for item in query:
        item.delete()
      
      # now delete the topic itself by calling the base delete
      db.Model.delete(self)
      
       
       
       
class TopicForm(djangoforms.ModelForm):
    class Meta:
        model = Topic 
        exclude = ['_class','author','group_id']

